//
//  VisitorLoginView.swift
//  TWeibo
//
//  Created by wtw on 15/7/29.
//  Copyright © 2015年 wtw. All rights reserved.
//

import UIKit

 /// 访客视图协议
protocol VisitorLoginViewDelegate: NSObjectProtocol {
    /// 访问视图将要登录
    func VisitorLoginViewWillLogin()
    ///访问视图将要注册
    func VisitorLoginViewWillRegister()
}

    //访客登录视图
class VisitorLoginView: UIView {
    
    /// 定义代理 - 一定要用 weak 
    weak var delegate: VisitorLoginViewDelegate?

    /// 按钮的监听方法
    func clickLoginButton() {
    delegate?.VisitorLoginViewWillLogin()
    }
    func clickRegisterButton() {
    delegate?.VisitorLoginViewWillRegister()
    }
    
    
    /// MARK: - 设置视图信息
    func setupInfo(isHome: Bool,imageName: String,Message: String) {
       
        messagelabel.text = Message
        
        iconView.image = UIImage(named: imageName)
        
        homeimageView.hidden = !isHome
        isHome ? startAnimation() : sendSubviewToBack(maskIconView)
        
    }
  
    /// 添加动画
    private func startAnimation() {
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 20.0
        
        //设置动画属性，切换界面不影响动画的执行
        anim.removedOnCompletion = false
        
        iconView.layer.addAnimation(anim, forKey: nil)
    }

    // MARK - 界面初始化
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        //fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
    }
    
    //设置界面元素
  private func setupUI() {
 /// 添加控件
    addSubview(iconView)
    addSubview(maskIconView)
    addSubview(homeimageView)
    addSubview(messagelabel)
    addSubview(registerButton)
    addSubview(loginButton)
    
    //设置视图背景颜色
    backgroundColor = UIColor(white: 0.93, alpha: 1.0)
    
    /// 设置自动布局
    ///图标的自动布局
    iconView.translatesAutoresizingMaskIntoConstraints = false
    addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
   addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: -40))
    /// 房子的自动布局
    homeimageView.translatesAutoresizingMaskIntoConstraints = false
    addConstraint(NSLayoutConstraint(item: homeimageView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
    addConstraint(NSLayoutConstraint(item: homeimageView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
     /// 描述的文字的布局
    messagelabel.translatesAutoresizingMaskIntoConstraints = false
    addConstraint(NSLayoutConstraint(item: messagelabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
    addConstraint(NSLayoutConstraint(item: messagelabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
    addConstraint(NSLayoutConstraint(item: messagelabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 224))
    /// 设置注册按钮的布局
    registerButton.translatesAutoresizingMaskIntoConstraints = false
    addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: messagelabel, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: 0))
    addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messagelabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
    addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
    addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 35))
    //设置登录按钮的布局
    loginButton.translatesAutoresizingMaskIntoConstraints = false
    addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messagelabel, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: 0))
    addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messagelabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
    addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
    addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 35))
    
    /// 设置遮罩的布局
    maskIconView.translatesAutoresizingMaskIntoConstraints = false
    addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[maskView]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["maskView": maskIconView]))
    addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[maskView]-200-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["maskView": maskIconView]))
    
    }
    
    /// 懒加载属性
    /// 图标
    private lazy var iconView: UIImageView = {
        let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        return iv
    }()
    /// 小房子图标
    private lazy var homeimageView: UIImageView = {
        let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        return iv
    }()
    /// 描述文字
    private lazy var messagelabel: UILabel = {
    
        let label = UILabel()
        label.text = "关注一些人，回这里看看有什么惊喜"
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(14.0)
        label.textAlignment = NSTextAlignment.Center
        label.numberOfLines = 0
        label.sizeToFit()
        
        return label
    }()
    
    /// 注册按钮
    private lazy var registerButton: UIButton = {
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.addTarget(self, action: "clickRegisterButton", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    /// 登录按钮
    private lazy var loginButton: UIButton = {
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitle("登录", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        btn.addTarget(self, action: "clickLoginButton", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
        }()
    /// 遮罩图片
    private lazy var maskIconView: UIImageView = {
    
        let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        return iv
    }()
}
